package org.academic.texteditor.gui.component;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JDesktopPane;
import javax.swing.event.MouseInputAdapter;

public class JGlassPanel extends JDesktopPane {

	private static final long serialVersionUID = -8024494787077480127L;

	private float opacity;

	public JGlassPanel() {
		this(0.4f);
	}
	
	public JGlassPanel(float opacity) {
		this.opacity = opacity;

		setLayout(null);
		setOpaque(false);
		
		MouseInputAdapter adapter = new MouseInputAdapter(){};
		
		addMouseListener(adapter);
		addMouseMotionListener(adapter);
		addMouseWheelListener(adapter);
	}

	@Override
	public Component add(Component component) {
		centralizeComponent(component);
		
		return super.add(component);
	}

	@Override
	public final void setOpaque(boolean isOpaque) {
		super.setOpaque(false);
	}
	
	@Override
	public final void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		
		Composite defaultComposite = g2d.getComposite();
		Color defaultColor = g2d.getColor();
		
		g.setColor(Color.BLACK);
		
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
        g2d.fillRect(0, 0, getWidth(), getHeight());

        g2d.setColor(defaultColor);
        g2d.setComposite(defaultComposite);
        
        super.paint(g);
	}

	public void centralizeComponent(Component component) {
		Dimension size = getSize();
		
		int x = (size.width / 2) - (component.getWidth() / 2);
		int y = (size.height / 2) - (component.getHeight() / 2);

		component.setLocation(x, y);
	}
	
}
